﻿using POC.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using wlog.Model;

namespace wlog.Shell.IO
{
    public class WebClientCommand : Command
    {
        string fileName = "";
        string location = "inline";
        int pageSize = 100;
        Queue<OutputChannel> _models = new Queue<OutputChannel>();
        TextWriter writer = null;
        public void SetModels(Queue<OutputChannel> models)
        {
            _models = models;
        }
        public override void Setup(string[] args)
        {
            try
            {
                TokenizeCommand(args,1);
            }
            catch (IndexOutOfRangeException exc)
            {
                Results.Add("Insufficent arguments passed.");
            }
            string uri = additionalCommands[K_OPTIONS.objecturi];
            fileName = additionalCommands[K_OPTIONS.objecturi];
            base.CreateTask(() =>
            {
                try{
                    WebClient client = new WebClient();                   
                    string downloads = client.DownloadString(uri);
                    Results.Add("Downloaded.." + uri);
                    Results.Add("Contents:");
                    Results.Add(downloads);
                }
                catch (Exception cxo)
                {
                    Console.WriteLine("Error in print, Message = " + cxo.Message);
                }
                finally
                {
                   
                }
            });
        }
        public void PrintVariable(string key, TextWriter writer)
        {
            string value = ApplicationConfiguration.Get(key);
            writer.WriteLine("{0}={1}", key, value);
        }
     
        public static void PrintConsole(OutputChannel model, TextWriter writer)
        {
            foreach (var line in model.Lines)
            {
                writer.WriteLine(line);
            }
            writer.WriteLine(Environment.NewLine);
        }
        public static void PrintConsole(List<OutputChannel> models, TextWriter writer)
        {
            foreach (var model in models)
            {
                PrintConsole(model, writer);
            }
        }

    }
}
